// pages/my/index.js
import {getXzqList, getInfo, productinfo} from '../../utils/request'
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.baseUrl,
    imageL:'',
    productname:'',
    userName:'',
    objectArray:[],
    productid:"",
    showtext: false, //显示textarea
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '领取奖品' 
    });
    this.setData({
      productid:options.productid,
      image: options.image,
      productname: options.productname
    });
    this.getList();
    this.getInfos();
  },
  
  async getInfos() {
    const data = await getInfo();
    
    this.setData({
      userName: data.data.real_name
    })
  },
  // 行政区域
  async getList() {
    const data = await getXzqList();
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({
        id: key,
        name: val.area
      })
    });
    this.setData({
      objectArray: {
        title: "请选择行政区域",
        array
      }
    })
  },

  // 隐藏textarea
  showmodel(e) {
    this.setData({
      showtext: e.detail
    })
  },
  updataaddress(e) {
    this.data.addrDetail = e.detail.value;
  },

  name(e) {
    this.data.userName = e.detail.value;
  },
  phone(e) {
    this.data.phone = e.detail.value;
  },

  updataQy(e) {
    this.setData({
      area: e.detail,
      showtext: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  

   saveData() {
    const func= async()=>{
      let data = await productinfo({
        productname: this.data.productname,
        userName: this.data.userName,
        mobile: this.data.phone,
        area: this.data.area,
        address: this.data.addrDetail,
        productid:this.data.productid
      });
      if(data.code === '1') {
        app.func.Toast.success("保存成功")
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/my/index'
          })
        }, 1000)
      }
    }
    wx.requestSubscribeMessage({
      tmplIds: ['bY3ze2a0tFUsIRuyRptvAFWJhg7BKnmnRTZfS3XCj0s','107tgLOct49jzx3ylrLLK3WHLtUokL3kbEfA7R1yOsU','AJXvR03taERIgAFBVS0iN8c4ypMRUL34LohsvdSSyMs'],
      success (res) { 
        func()
      },
      fail:(error)=>{
        func()
      }
    })
    
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})